package com.xxh.market.controller;

import com.xxh.market.dao.OrderDao;
import com.xxh.market.dao.impl.OrderDaoImpl;
import com.xxh.market.entity.Order;
import com.xxh.market.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


//订单相关请求
@WebServlet(name = "OrderServlet",urlPatterns = "/order/*")
public class OrderServlet extends HttpServlet {

    OrderDao orderDao=new OrderDaoImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String pathInfo = request.getPathInfo();
        HttpSession session = request.getSession();
        //获取用户订单
        if(pathInfo.equals("/selectByUser")){
            //拿到用户ID
            User userDetail = (User)session.getAttribute("userDetail");
            Integer uid = userDetail.getId();
            //根据ID查询订单
            List<Order> orders = orderDao.selectOrdersByUser(uid);
            //订单数据放入session
            session.setAttribute("orders",orders);
            response.sendRedirect(request.getContextPath()+"/orders.jsp");

        }
    }
}
